<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/13
 * Time: 15:51
 */

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class Industry_MxShopPage extends WebPage {

    public function main() {
        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;
        $keyword = $_GPC['keyword'];
        $condition = '';
        if (!empty($keyword)) {
            $condition .= " AND i.name LIKE '%{$keyword}%' ";
        }
        $status = intval($_GPC['status']);
        if (!empty($status)) {
            if ($status == 2) {
                $status2 = 0;
            } else {
                $status2 = $status;
            }
            $condition .= ' AND i.status=' . $status2;
        }

        $list = pdo_fetchall("SELECT i.*, p.name as pname FROM " . tablename('mx_shop_mendian_industry') . " i LEFT JOIN ".tablename('mx_shop_platform')." p ON i.platformid=p.id WHERE i.uniacid = " . $uniacid . $condition . " AND i.deleted=0 ORDER BY i.displayorder DESC,i.id ASC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
        $total = pdo_fetchcolumn("SELECT count(i.id) FROM " . tablename('mx_shop_mendian_industry') . " i WHERE i.uniacid=" . $uniacid . " AND i.deleted=0" . $condition);

        $pager = pagination($total, $pindex, $psize);

        include $this->template();
    }

    public function post(){

        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $id = intval($_GPC ['id']);

        if (!empty ($id)) {
            $item = pdo_fetch("SELECT * FROM " . tablename('mx_shop_mendian_industry') . " WHERE id = '$id'");
        } else {
            $item = array(
                'displayorder' => 0,
                'thumb' => '',
                'status' => 1
            );
        }

        if ($_W['ispost']) {

            if (empty ($_GPC ['catename'])) {
                show_json(0, '抱歉，请输入门店行业名称！');
            }

            $pid = intval($_GPC ['platformid']);
            if (empty ($pid)) {
                show_json(0, '抱歉，请选择行业所属平台！');
            }

            $platform = pdo_fetch("SELECT id FROM " . tablename('mx_shop_platform') . " WHERE id = :id AND deleted=0", array(':id' => $pid));
            if(empty($platform['id'])){
                show_json(0,"平台不存在");
            }

            $mcname = pdo_fetch("SELECT id FROM " . tablename('mx_shop_mendian_industry') . " WHERE id != :id AND name = :mcname AND deleted=0", array(':id' => $id, ':mcname' => $_GPC['catename']));
            if(!(empty($mcname['id']))){
                show_json(0,"该门店行业名已存在");
            }

            $data = array(
                'uniacid' => $uniacid,
                'platformid' => $pid,
                'name' => $_GPC ['catename'],
                'description' => $_GPC ['description'],
                'status' => intval($_GPC ['status']),
                'thumb' => $_GPC ['thumb'],
                'displayorder' => intval($_GPC ['displayorder']),
                'updatetime' => TIMESTAMP
            );
            if (!empty ($id)) {
                pdo_update('mx_shop_mendian_industry', $data, array('id' => $id));
                plog('platform.industry.add', '更新"' . $data['name'] . '"门店行业成功！门店行业ID为' . $id);
            } else {
                $data['createtime'] = $data['updatetime'];
                pdo_insert('mx_shop_mendian_industry', $data);
                $id = pdo_insertid();
                plog('platform.industry.add', '新建"' . $data['name'] . '"门店行业成功！门店行业ID为' . $id);
            }
            show_json(1, array('url'=>webUrl('platform/industry')));

        }

        $platform = pdo_fetchall('SELECT id,name FROM '.tablename('mx_shop_platform').' WHERE uniacid=:uniacid AND deleted=0', array(':uniacid'=>$uniacid));
        include $this->template();
    }

    public function status(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        $status = intval($_GPC['status']);

        if(!empty($id)){
            if($status == 1){
                pdo_update('mx_shop_mendian_industry', array( 'status' => 1), array('id' => $id));
            }else{
                pdo_update('mx_shop_mendian_industry', array( 'status' => 0), array('id' => $id));
            }
        }
        show_json(1, array('url'=>webUrl('platform/industry')));

    }

    public function mdcatedel(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        if (empty($id)) {
            $id = is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0;
        }

        $md = pdo_fetchall("SELECT id FROM " . tablename('mx_shop_mendian') . " WHERE industryid in( $id ) AND deleted=0 AND uniacid=" . $_W['platformid']);
        if(!empty($md)){
            show_json(0, '要删除该行业，需删除该行业下的门店');
        }

        $cates = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_mendian_industry') . " WHERE id in( $id ) AND uniacid=" . $_W['platformid']);

        foreach ($cates as $cate) {
            pdo_update('mx_shop_mendian_industry', array('deleted' => 1, 'status' => 0), array('id' => $cate['id']));
            plog('platform.industry.delete', "删除门店行业 <br/>行业信息:  ID: {$cate['id']}");
        }
        show_json(1, array('url' => referer()));

    }

    public function bonus(){

        global $_W,$_GPC;
        $uniacid = $_W['platformid'];
        $id = intval($_GPC['id']);

        if(empty($id)){
            show_json(0,'参数有误！');
        }

        $industry = pdo_fetch("SELECT id,name FROM " . tablename('mx_shop_mendian_industry') . " WHERE uniacid=:uniacid AND id = :id AND deleted=0", array(':uniacid'=>$uniacid,':id' => $id));
        if(empty($industry)){
            show_json(0,'抱歉，该行业不存在！');
        }
        $item = pdo_fetch("SELECT * FROM " . tablename('mx_shop_mendian_bonus') . " WHERE uniacid=:uniacid AND industryid = :industryid ", array(':uniacid'=>$uniacid, ':industryid' => $id));

        if ($_W['ispost']) {
            $num = abs($_GPC['tuijian2']) + abs($_GPC['tuijian3']) + abs($_GPC['shengji']) + abs($_GPC['quyu']) + abs($_GPC['hangye']) + abs($_GPC['jishu']) + abs($_GPC['tuijian']) + abs($_GPC['xiaofei']) + abs($_GPC['gongsi']) + abs($_GPC['jiangjin']);
            if(ceil($num) > 100){
                show_json(0,'行业分红不能超过100%！');
            }

            $data = array(
                'uniacid' => $uniacid,
                'shengji' => abs($_GPC['shengji']),
                'quyu' => abs($_GPC['quyu']),
                'hangye' => abs($_GPC['hangye']),
                'jishu' => abs($_GPC['jishu']),
                'tuijian' => abs($_GPC['tuijian']),
                'tuijian2' => abs($_GPC['tuijian2']),
                'tuijian3' => abs($_GPC['tuijian3']),
                'xiaofei' => abs($_GPC['xiaofei']),
                'gongsi' => abs($_GPC['gongsi']),
                'jiangjin' => abs($_GPC['jiangjin']),
                'updatetime' => time()
            );

            if (empty($item['id'])) {
                $data['industryid'] = $id;
                $data['createtime'] = time();
                pdo_insert('mx_shop_mendian_bonus', $data);
                $bid = pdo_insertid();
                plog('platform.industry.bonus', '新建"' . $industry['name'] . '"行业分红成功！行业分红ID为' . $bid);
            } else {
                pdo_update('mx_shop_mendian_bonus', $data, array('id' => $item['id']));
                plog('platform.industry.bonus', '更新"' . $industry['name'] . '"行业分红成功！行业分红ID为' . $item['id']);
            }
            show_json(1, array('url'=>webUrl('platform/industry')));
        }

        include $this->template();

    }


}
